package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.Common.R;
import com.wyx.DTO.SetmealDto;
import com.wyx.Pojo.Category;
import com.wyx.Pojo.Setmeal;
import com.wyx.Pojo.SetmealDish;
import com.wyx.Service.CategoryService;
import com.wyx.Service.SetmealDishService;
import com.wyx.Service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private RedisTemplate redisTemplate;
//    @Autowired
//    private CacheManager cacheManager;
    /**
     * 用户端需要的套餐信息
     * @param setmeal
     * @return
     */
//    @Cacheable(value = "SetmealCache",key = "#setmeal.categoryId")
    @GetMapping("/list")
    public R<List<Setmeal>> getlistbyfront(Setmeal setmeal){
        Integer status = setmeal.getStatus();
        Long categoryId = setmeal.getCategoryId();
        String key= "setmeal_"+categoryId;
        //查看redis缓存有没有这个 如果有则直接返回
        List<Setmeal> o = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (o!=null){
            return R.success(o);
        }
        LambdaQueryWrapper<Setmeal>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(wrapper);
        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }
    /**
     * 服务端的分页查询 根据名称查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> getpage(int page, int pageSize, String name) {
        //但是return的是categoryid 并不是name
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        setmealService.page(page1, wrapper);
        List<Setmeal> records = page1.getRecords();
        Page<SetmealDto> pageres = new Page<>();
        //categoryname是null
        List<SetmealDto> reslist = new ArrayList<>();
        for (Setmeal record : records) {
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            //将record的一个复制到setmealdto中
            setmealDto.setCategoryName(categoryName);
            reslist.add(setmealDto);
        }
        pageres.setRecords(reslist);
        return R.success(pageres);
    }

    /**
     * 服务端新增套餐(一个)
     *
     * @param setmealDto 这里面包含了Setmeal 还要Setmealdish的关系
     * @return
     */
    @PostMapping
    public R<String> addnewSetmeal(@RequestBody SetmealDto setmealDto) {
        System.out.println(setmealDto);

        setmealService.AddnewSetmeal(setmealDto);
        //当setmealDto被添加后 setmealDto的Setmealid就有了
        Long categoryId = setmealDto.getCategoryId();
        String key= "setmeal_"+categoryId;
//        //将缓存给删了
        redisTemplate.delete(key);
        return R.success("新增成功");
    }

    /**
     * 服务端的批量停售(多个或一个)
     *
     * @param ids
     * @return
     */
//    @CacheEvict(value = "SetmealCache",allEntries = true)//删除你缓存的名称下所有的key
    @PostMapping("/status/0")
    public R<String> downstatus(Long[] ids) {
        //将ids数组的套餐全部查出来 然后修改里面的Status
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(0);
//            //将缓存给删了
            redisTemplate.delete("setmeal_"+setmeal.getCategoryId());
        }
        //将List数组交给Mybatisplus一起Update
        setmealService.updateBatchById(setmeals);

        return R.success("修改成功~");
    }

    /**
     * 服务端的批量启售(多个或一个)
     *
     * @param ids
     * @return
     */
//    @CacheEvict(value = "SetmealCache",allEntries = true)//删除你缓存的名称下所有的key
    @PostMapping("/status/1")
    public R<String> upstatus(Long[] ids) {
        //将ids数组的套餐全部查出来 然后修改里面的Status
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
//            //删除缓存
            redisTemplate.delete("setmeal_"+setmeal.getCategoryId());
        }
        //将List数组交给Mybatisplus一起Update
        setmealService.updateBatchById(setmeals);
        return R.success("修改成功~");
    }

    /**
     * 删除跟批量删除(多个或一个)
     * 要操作两个表Setmeal跟Setmealdish表
     *
     * @param ids
     * @return
     */
//    @CacheEvict(value = "SetmealCache",allEntries = true)//删除你缓存的名称下所有的key
    @DeleteMapping
    public R<String> deletebyids(Long[] ids) {
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
//            //删除缓存
            redisTemplate.delete("setmeal_"+setmeal.getCategoryId());
        }
        setmealService.deleteSetmealanddish(ids);
        return R.success("删除成功~");
    }

    /**
     * 服务端回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getone(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, dto);
        dto.setSetmealDishes(list);
        return R.success(dto);
    }

    /**
     * 服务端修改套餐(一个)
     * @param dto
     * @return
     */
//    @CacheEvict(value = "SetmealCache",key = "#dto.categoryId")
    @PutMapping
    public R<String> Update(@RequestBody SetmealDto dto) {
        setmealService.UpdateSetmealandDish(dto);
        Long categoryId = dto.getCategoryId();
        //将缓存删了
        redisTemplate.delete("setmeal_"+categoryId);
        return R.success("修改成功~");
    }
}
